package com.astn.bks;

import com.astn.bks.util.BksConstants;
import com.astn.entity.booking.QuoteBed;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class DiaBed extends Activity {
	private Intent contentIntent;
	private int state;
	private QuoteBed cbed;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		contentIntent = getIntent();

		state = contentIntent.getIntExtra(BksConstants.EXTRA_STATE,
				BksConstants.ADD);
		switch (state) {
		case BksConstants.ADD:
			setTitle("Add new bed");
			break;
		case BksConstants.EDIT:
			setTitle("Edit bed");
			cbed = (QuoteBed) contentIntent
					.getSerializableExtra(BksConstants.EXTRA_BED);
			break;
		default:
			break;
		}
		setContentView(R.layout.diabed);
		getWindow().setLayout(LayoutParams.FILL_PARENT,
				LayoutParams.WRAP_CONTENT);
		Button btnOK = (Button) findViewById(R.id.btnOK);
		Button btnCancel = (Button) findViewById(R.id.btnCancel);
		final EditText txtBed = (EditText) findViewById(R.id.txtBedType);
		final EditText txtCount = (EditText) findViewById(R.id.txtCount);
		if (cbed != null) {
			txtBed.setText(cbed.getName());
			txtCount.setText(String.valueOf(cbed.getQuantity()));
		}
		btnOK.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent data = new Intent();
				String type = txtBed.getText().toString();
				int count = Integer.parseInt(txtCount.getText().toString());
				if (type.trim().length() > 0 && count > 0) {
					cbed = new QuoteBed();
					cbed.setQuantity(count);
					cbed.setName(type);
					data.putExtra(BksConstants.EXTRA_BED, cbed);
					data.putExtra(BksConstants.EXTRA_STATE, state);
					setResult(Activity.RESULT_OK, data);
					finish();
				} else {
					Toast.makeText(DiaBed.this,
							"Bed type and count is require", Toast.LENGTH_SHORT)
							.show();
				}
			}

		});
		btnCancel.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent data = new Intent();
				setResult(Activity.RESULT_CANCELED, data);
				finish();
			}

		});
	}

}
